﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class CoLoader
    {
        public long ID = -1;
        public string Carr = "";
        public string Port = "";
        public string Pic_sales = "";
        public string Tel = "";
        public string Cell = "";
        public string Charge = "";
        public string Skype = "";
        public string Email = "";
        public string Description = "";
        public int Sort = 0;
        public long PicID = 0;
        public DateTime LastUpdate = DateTime.Now;
        public CoLoader()
        {
        }

        public CoLoader(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_coloader";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                             "`Carr`" +
                             ",`Port`" +
                             ",`Pic_sales`" +
                             ",`PicID`" +
                             ",`Tel`" +
                             ",`Cell`" +
                             ",`Charge`" +
                             ",`Skype`" +
                             ",`Email`" +
                             ",`Description`" +
                             ",`Sort`" +
                             ",`LastUpdate`" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(Carr) + "'" +
                             ",'" + DBModule.RefineString(Port) + "'" +
                             "," + "'" + DBModule.RefineString(Pic_sales) + "'" +
                             ","  + PicID  +
                             "," + "'" + DBModule.RefineString(Tel) + "'" +
                             "," + "'" + DBModule.RefineString(Cell) + "'" +
                             "," + "'" + DBModule.RefineString(Charge) + "'" +
                             "," + "'" + DBModule.RefineString(Skype) + "'" +
                             "," + "'" + DBModule.RefineString(Email) + "'" +
                             "," + "'" + DBModule.RefineString(Description) + "'" +
                             "," + Sort.ToString() +
                             "," + DBModule.RefineDatetime(LastUpdate) +
                             ")";
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CoLoader", "add", cn, trans);
                //GV.Response.Write(strSQL);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "Carr='" + DBModule.RefineString(Carr) + "'" +
                            ",Port='" + DBModule.RefineString(Port) + "'" +
                             ",Pic_sales='" + DBModule.RefineString(Pic_sales) + "'" +
                             ",PicID=" + PicID + 
                             ",Tel='" + DBModule.RefineString(Tel) + "'" +
                             ",Cell='" + DBModule.RefineString(Cell) + "'" +
                             ",Charge='" + DBModule.RefineString(Charge) + "'" +
                             ",Skype='" + DBModule.RefineString(Skype) + "'" +
                             ",Email='" + DBModule.RefineString(Email) + "'" +
                             ",Description='" + DBModule.RefineString(Description) + "'" +
                             ",Sort = " + Sort.ToString() +
                             ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("CoLoader", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_coloader WHERE ID = " + this.ID.ToString();

            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("CoLoader", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_coloader WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Carr")) Carr = dr["Carr"].ToString();
                if (!dr.IsNull("Port")) Port = dr["Port"].ToString();
                if (!dr.IsNull("Pic_sales")) Pic_sales = dr["Pic_sales"].ToString();
                if (!dr.IsNull("PicID")) PicID =long.Parse(dr["PicID"].ToString());
                if (!dr.IsNull("Tel")) Tel = dr["Tel"].ToString();
                if (!dr.IsNull("Cell")) Cell = dr["Cell"].ToString();
                if (!dr.IsNull("Charge")) Charge = dr["Charge"].ToString();
                if (!dr.IsNull("Skype")) Skype = dr["Skype"].ToString();
                if (!dr.IsNull("Email")) Email = dr["Email"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
            }
        }

        public static void GetList(string strIDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_coloader where 1=1 ";
            if ((strIDs != "") && (strIDs != null))
            {
                if (strIDs != "All")
                    strSQL += strIDs;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write("sql=" + strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_coloader where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}
